package cn.edu.lingnan.part.listener;

import cn.edu.lingnan.part.entity.Student;
import cn.edu.lingnan.part.entity.StudentExcel;
import cn.edu.lingnan.part.service.StudentService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: chen_man_cheng
 * @Description: //TODO
 * @Date: Create in 15:26 2020/5/31
 */
public class StudentExcelListener extends AnalysisEventListener<Student> {
//    private static final Logger LOGGER = LoggerFactory.getLogger(StudentExcelListener.class);
//
//    /**
//     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
//     */
//    private static final int BATCH_COUNT = 5;

//    用来保存读到的数据
    List<Student> data = new ArrayList<Student>();

    private StudentService studentService;
    public StudentExcelListener(StudentService studentService){
        this.studentService = studentService;
    }

    @Override
    public void invoke(Student student, AnalysisContext analysisContext) {
//        解析数据保存到StudentExcel
        data.add(student);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//当excel解析完成后，执行
//        LOGGER.info("存储数据库成功！");
        System.out.println(data);
        //这里是一个批量插入
        if(studentService != null){
            studentService.insert(data);

        }

    }
}
